//
//  HTWaybillCell.m
//  TruckDriver
//
//  Created by Ting Han on 2017/9/28.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTWaybillCell.h"
#import "HTCommentView.h"

@interface HTWaybillCell()<HTCommentViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *startLabel;
@property (weak, nonatomic) IBOutlet UILabel *endLabel;
@property (weak, nonatomic) IBOutlet UIView *tapView;
@property (weak, nonatomic) IBOutlet UILabel *tapLabel;
@property (weak, nonatomic) IBOutlet UIImageView *tapImageView;

@property (weak, nonatomic) IBOutlet UILabel *goodsTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *stateLabel;

@end

@implementation HTWaybillCell

- (void)setWaybillModel:(HTWaybillModel *)waybillModel{
    _waybillModel = waybillModel;
    NSString *length = [HTUtils sortLengthDisplayStringWith:waybillModel.heiht];
    NSString *type = (waybillModel.goodstype == nil || waybillModel.goodstype.length <= 0)?@"不限车型": waybillModel.goodstype;
    NSString *dun = @"";
    if ([waybillModel.unti isEqualToString:@"1"]) {
        dun =[NSString stringWithFormat:@"%@吨",waybillModel.weight];
    }else{
        dun =[NSString stringWithFormat:@"%@方",waybillModel.weight];
    }
    NSString *cartype = @"";
    if (!(waybillModel.cartype == nil || waybillModel.cartype.length <= 0)) {
        cartype = waybillModel.cartype;
    }
    self.goodsTypeLabel.text = [NSString stringWithFormat:@"%@ %@ / %@ %@ / 运费%@元",cartype,dun,length,type,waybillModel.pricers];
    self.startLabel.text = waybillModel.fromadd;
    self.endLabel.text = waybillModel.toadd;
    self.timeLabel.text = [HTUtils timeIntervalExchange:waybillModel.createtime];
    self.nameLabel.text = waybillModel.drivername;
    if ([waybillModel.state isEqualToString:@"0"]) {
        //未接单
        self.stateLabel.text = @"已接单";
        self.tapLabel.text = @"联系货主";
        self.tapImageView.image = [UIImage imageNamed:@"icon_list_call"];
    }else if ([waybillModel.state isEqualToString:@"1"]){
        //进行中
        self.stateLabel.text = @"运输中";
        self.tapLabel.text = @"联系货主";
        self.tapImageView.image = [UIImage imageNamed:@"icon_list_call"];
    }else if ([waybillModel.state isEqualToString:@"2"]){
        //待评价
        self.stateLabel.text = @"待评价";
        self.tapLabel.text = @"评价货主";
        self.tapImageView.image = [UIImage imageNamed:@"icon_list_comment"];
    }else if ([waybillModel.state isEqualToString:@"3"]){
        //已完成
        self.stateLabel.text = @"已完成";
        self.tapLabel.text = @"查看详情";
        self.tapImageView.image = [UIImage imageNamed:@"icon_list_detail"];
    }else if ([waybillModel.state isEqualToString:@"4"]){
        //已取消
        self.stateLabel.text = @"已取消";
        self.tapLabel.text = @"查看详情";
        self.tapImageView.image = [UIImage imageNamed:@"icon_list_detail"];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didCallDetial:)];
    [self.tapView addGestureRecognizer:tap];
    
}
- (void)didCommitComment:(NSString *)message scroll:(NSString *)scroll orderid:(NSString *)orderid commentView:(HTCommentView *)commentView{
    YDHUD *hud = [YDHUD juhuaHUD];
    [self.controller.view addSubview:hud];
    [hud showAnimated:YES];
    [HTHttpRequestManager requestWayBillCommentWithOrderid:self.waybillModel.orderid scorll:scroll impress:message success:^(id  _Nullable responseObject) {
        [hud hideAnimated:YES];
        DBLog(@"提交评论：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"评价失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
           [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
         [HTShowUtil showMessage:responseObject[@"message"]];
        [commentView dismissView];
        if (self.refreshBlock) {
            self.refreshBlock();
        }
    } failure:^(NSError * _Nonnull error) {
        [hud hideAnimated:YES];
        [HTShowUtil showMessage:@"评价失败"];
    }];
}
- (void)didCallDetial:(UITapGestureRecognizer*)sender{
    switch ([self.waybillModel.state integerValue]) {
        case 0:
        {
            //未接单
            NSString *phone = self.waybillModel.phone;
            NSString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            return;
        }
            break;
        case 1:
        {
            //进行中
            NSString *phone = self.waybillModel.phone;
            NSString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            return;
        }
            break;
        case 2:
        {
            //待评价
            HTCommentView *comentView = [[HTCommentView alloc]init];
            [comentView showView];
            comentView.delegate = self;
            return;
        }
            break;
        default:
            break;
    }
    if (self.detialBlock) {
        self.detialBlock(self.waybillModel);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end
